package com.sample;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.clj.fastble.data.BleDevice;
import com.ivsign.lib_idcardidentification.DeviceUUID;
import com.ivsign.lib_idcardidentification.IDCardIdentificationManager;
import com.ivsign.lib_idcardidentification.IDCardInformation;
import com.ivsign.lib_idcardidentification.IReadListener;
import com.ivsign.lib_idcardidentification.ProgressResult;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

public class ReadIDSampleActivity extends Activity {
    private String LOGTAG = "BTidr";
    public static boolean isRecording = false;// 线程控制标记

    private static boolean debug = true;
    private static boolean debug_tmp = true;
    private byte[] recv_buf = new byte[1423];

    private static String service = "0000e455-0000-1000-8000-00805f9b34fb";
    private static String writeChracter = "49535343-8841-43f4-a8d4-ecbe34729bb3";

    private static String readChracter = "49535343-1e4d-4bd9-ba61-23c647249616";


    private Socket Tcpsocket = null;    // = new Socket();
    private OutputStream TcpSocketOut = null;
    private InputStream TcpSocketIn = null;

    private Button releaseCtrl;
    private EditText _txtRead;


    private String encodeType = "GBK";



    static byte[] manfile = null;

    private InputStream cwjInStream;
    private OutputStream cwjOutStream;

    TextView ett;
    ImageView image;


    private ProgressBar pb;
    private String mac;
    private BleDevice bleDevice;
    private TextView tv_times;
    private TextView tv_command;
    private DeviceUUID deviceUUID;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainid);

        mac = getIntent().getStringExtra(Constant.mac);
        bleDevice = getIntent().getParcelableExtra(Constant.serviceData);

        tv_times = (TextView) findViewById(R.id.tv_times);
        tv_command = (TextView) findViewById(R.id.tv_command);

        List<BLEDeviceInfo.BleBean> ble = BLEApplication.getBle();
        for (BLEDeviceInfo.BleBean bleBean : ble) {
            if (mac.startsWith(bleBean.getMac())){
                deviceUUID = new DeviceUUID();
                deviceUUID.setMac(mac);
                deviceUUID.setService(bleBean.getService());
                deviceUUID.setNotifyChracter(bleBean.getRead());
                deviceUUID.setWriteChracter(bleBean.getWrite());
                break;
            }
            if (bleBean.getMac().equals("*")){
                deviceUUID.setMac(mac);
                deviceUUID.setService(bleBean.getService());
                deviceUUID.setNotifyChracter(bleBean.getRead());
                deviceUUID.setWriteChracter(bleBean.getWrite());
                break;

            }

        }

        IDCardIdentificationManager.getInstance().readId(bleDevice, deviceUUID, new IReadListener() {
            @Override
            public void onRead(ProgressResult progress) {

                pb.setProgress(progress.getProgress());
                tv_command.setText("当期执行命令："+progress.getCommand());
            }

            @Override
            public void onSuccess(IDCardInformation decodeInfo) {
                {
                    if (decodeInfo != null) {
                        ett.setText("姓名：" + decodeInfo.getName() + "\n" + "性别：" + decodeInfo.getSex() + "\n" + "民族：" + decodeInfo.getNation() + "\n"
                                + "出生日期：" + decodeInfo.getBirthDate() + "\n" + "地址：" + decodeInfo.getAddress() + "\n" + "身份号码：" + decodeInfo.getiDCardNum() + "\n"
                                + "签发机关：" + decodeInfo.getSigningIssuingOrganization() + "\n" + "有效期限：" + decodeInfo.getStartDate() + "-" + decodeInfo.getEndDate() + "\n"
                                + "\n");

                        if (decodeInfo.getPortrait() != null) {
                            image.setImageBitmap(decodeInfo.getPortrait());
                        } else {
                            ett.append("照片解码失败，请检查路径" + Environment.getExternalStorageDirectory() + "/wltlib/");
                            image.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.faceid));
                        }

                    }
                }
            }

            @Override
            public void OnFailed(int times) {
                tv_times.setText("读卡失败次数："+times);
            }
        }, this);
        Button btsleep = (Button) findViewById(R.id.btsleep);
        btsleep.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                pb.setProgress(0);
                ett.setText("");
               // mHandler.setReadingflag(false);

                IDCardIdentificationManager.getInstance().reset();
                image.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.faceid));
            }
        });


//        BleManager.getInstance().notify(bleDevice
//                ,
//                service,
//                readChracter,
//                as);
        ett = (TextView) findViewById(R.id.textViewid1);
        image = (ImageView) findViewById(R.id.imageViewid1);
        pb = (ProgressBar) findViewById(R.id.progressBar2);
        pb.setMax(100);


//        // 激活读取线程
//        mHandler.obtainMessage(44).sendToTarget();


    }

    @Override
    protected void onResume() {
        super.onResume();

    }


    // }


    @Override
    public void onDestroy() {
        try {
            //as = null;
            // mHandler.removeMessages(44);
            IDCardIdentificationManager.getInstance().disconnect();
          //  BleManager.getInstance().disconnect(bleDevice);

        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }
}